import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.logging.Level;

public class MyDialog extends JDialog {
	ReaderFrame frame;
	Model model;
	Controller control;

	protected MyDialog(ReaderFrame frame, Model model, Controller control) {
		super(frame);
		this.frame = frame;
		this.model = model;
		this.control = control;
		setSize(200, 200);
		setLocation((frame.getLocation().x) / 2, frame.getLocation().y / 2);
		//setLocation(MouseInfo.getPointerInfo().getLocation());
	}

	protected MyDialog(ReaderFrame frame) {
		super(frame);
		this.frame = frame;
		setSize(200, 200);
		setLocation(MouseInfo.getPointerInfo().getLocation());
	}

	/**
	 * 警告框
	 *
	 * @param message 消息内容
	 * @return
	 */
	JDialog warning(String message) {
		setLayout(new FlowLayout());
		setTitle("警告");
		JLabel label = new JLabel(message);
		setSize(label.getSize());
		add(label);
		model.logger.log(Level.WARNING, message);
		return this;
	}

	/**
	 * 等待框
	 * @param Tittle 标题
	 * @param message 消息内容
	 * @return 等待框
	 */
	JDialog wait(String Tittle, String message) {// 等待模式
		JProgressBar progress_bar = new JProgressBar();
		progress_bar.setString(message);
		progress_bar.setStringPainted(true);
		progress_bar.setIndeterminate(true);
		setLayout(new FlowLayout());
		setSize(progress_bar.getSize());
		setTitle(Tittle);
		return this;
	}

	/**
	 * 添加书签
	 * @return 显示添加框
	 */
	JDialog addMark() {// 添加书签模式
		setLayout(new BorderLayout());
		setTitle("为" + model.file.getName() + "添加书签");
		JLabel label = new JLabel("在下方输入书签名");
		JTextField field = new JTextField(10);
		JButton add = new JButton("添加");
		JButton cancel = new JButton("取消");

		cancel.addActionListener(e -> dispose());
		add.addActionListener(e -> {
			String mark_name = field.getText();
			if (!mark_name.isEmpty()) {
				model.book_mark_map.put(mark_name, frame.text_area.getCaretPosition());
				//System.out.println(model.book_mark_map.toString());
				model.logger.info("add mark " + field.getText() + "," + mark_name + " to " + model.file.getName());
			} else {
				model.logger.log(Level.WARNING, "mark name is empty!");
			}
			dispose();
		});
		JPanel panel = new JPanel();
		FlowLayout layout = new FlowLayout();
		layout.setAlignment(FlowLayout.CENTER);
		panel.setLayout(layout);
		panel.add(add);
		panel.add(cancel);
		add(field, BorderLayout.CENTER);
		add(panel, BorderLayout.SOUTH);
		add(label, BorderLayout.NORTH);
		setSize(250, 120);
		setResizable(false);
		return this;
	}

	/**
	 * 显示书签框
	 * @return 书签框
	 */
	JDialog listMark() {// 显示书签模式
		setLayout(new BorderLayout());
		setTitle(model.file.getName() + "的书签");
		JList<String> list = new JList<>();
		DefaultListModel<String> list_model = new DefaultListModel<>();
		JPopupMenu menu = new JPopupMenu();
		JMenuItem del_mark = new JMenuItem("删除书签");
		list.setAutoscrolls(true);
		list.setCellRenderer(Tools.listCellRenderer);
		for (String s : model.book_mark_map.keySet()) {
			list_model.addElement(s);
		}
		list.setModel(list_model);
		menu.add(del_mark);
		del_mark.addActionListener(e -> {
			if (!list.isSelectionEmpty()) {
				model.logger.info("remove mark " + list.getSelectedValue());
				model.book_mark_map.remove(list.getSelectedValue());
				list_model.remove(list.getSelectedIndex());
				list.remove(0);
				Settings.apply();
				list.revalidate();
				list.repaint();
				list.validate();
				model.logger.info("remove success.");
			}
		});
		list.addListSelectionListener(e -> {
			Number position = model.book_mark_map.get(list.getSelectedValue());
			frame.text_area.setCaretPosition((position != null) ? position.intValue() : 0);
			if (position != null) {
				model.logger.info("Jump to " + list.getSelectedValue() + "," + position.intValue());
			}
		});
		list.addMouseListener(new MouseInputAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
				if (e.getButton() == MouseEvent.BUTTON3) {
					menu.show(list, e.getX(), e.getY());
				}
			}
		});
		add(list, BorderLayout.CENTER);
		return this;
	}
}
